//    Time Surfer
//    Copyright (C) 2008  Gaian Mystery School
//    http://www.gaianmysteryschool.com
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>

#import "TimeKit.h"


@implementation TimeKit
NSTimeInterval oneDay = (24 * 60 * 60);

char *MAYAN_TZOLKIN_MONTHS[] = {"Imix", "Ik'", "Ak'bal", "K'an", "Chikchan",
                                     "Kimi", "Manik'", "Lamat", "Muluk", "Ok",
                                     "Chuwen", "Eb", "Ben", "Ix", "Men",
                                     "Kib", "Kaban", "Ets'nab", "Kawak", "Ajaw"};

char *DREAMSPELL_TZOLKIN_MONTHS[] = {"Dragon", "Wind", "Night", "Seed", "Serpent",
                                     "World Bridger", "Hand", "Star", "Moon", "Dog",
                                     "Monkey", "Human", "Skywalker", "Wizard", "Eagle",
                                     "Warrior", "Earth", "Mirror", "Storm", "Sun"};

char *DREAMSPELL_TZOLKIN_COLORS[] = {"Red", "White", "Blue", "Yellow",
									 "Red", "White", "Blue", "Yellow",
									 "Red", "White", "Blue", "Yellow",
									 "Red", "White", "Blue", "Yellow",
									 "Red", "White", "Blue", "Yellow"};







char *DREAMSPELL_TONES[] = {"Magnetic", "Lunar", "Electric", "Self-Existing", "Overtone",
                                     "Rhythmic", "Resonant", "Galatic", "Solar", "Planetary",
                                     "Spectral", "Crystal", "Cosmic"};

char *TONESPULSATIONRAYS[] = {"Unity", "Polarity", "Rhythm", "Measure", "the Center",
                                     "Organic Balance", "Mystic Power", "Harmonic Resonance", "Cyclic Periodicity", "Manisfesation",
                                     "Disosonant Structure", "Complex Stabilty", "Universal Movement"};


char *TONESMODES[] = {"Unity", "Polarity", "Action", "Stability", "Empowerment",
                                     "Flow", "Reflective", "Justice", "Patience", "Manifestation",
                                     "Resolution", "Understanding", "Ascension"};

char *TONESWORDS[] = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen"};



char *MAYAN_HAAB_MONTHS[] = {"Pop", "Wo", "Sip", "Sots'", "Sek", "Xul",
                                  "Yaxk'in", "Mol", "Ch'en", "Yax", "Sak", "Keh",
                                  "Mak", "K'ank'in", "Muwan", "Pax", "K'ayab", "Kumk'u", "Wayeb"};


#define LONGCOUNT_DAYS  1872000
#define FOURSOLARYEARS 1460.9688

+(NSCalendarDate*)endDate
{
	NSString *kBookmarkYear=@"Year";
	NSString *kBookmarkMonth=@"Month";
	NSString *kBookmarkDay=@"Day";
	NSString *kEndDateKey=@"End Date Key";

	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	NSDictionary *dict=[defaults dictionaryForKey: kEndDateKey];
	if (dict)
		return [NSCalendarDate dateWithYear:[[dict objectForKey:kBookmarkYear] intValue] month:[[dict objectForKey:kBookmarkMonth] intValue] day:[[dict objectForKey:kBookmarkDay] intValue] hour:00 minute:00 second:00 timeZone:[NSTimeZone systemTimeZone]];

	return [NSCalendarDate dateWithYear:2012 month:12 day:21 hour:00 minute:00 second:00 timeZone:[NSTimeZone systemTimeZone]];
}

+(NSCalendarDate*)startDate
{
	NSCalendarDate *endDate = [TimeKit endDate];

	NSTimeInterval nst=((LONGCOUNT_DAYS*oneDay)*-1);

	return [endDate addTimeInterval:nst];
}

-(int)longCount:(int)baktun katun:(int)katun tun:(int)tun uinal:(int)uinal kin:(int)kin
{
    return (baktun * 144000) +
	(katun  *   7200) +
	(tun    *    360) +
	(uinal  *     20) +
	kin;
}

-(NSCalendarDate*)longCountToNSCalendarDate:(int)baktun katun:(int)katun tun:(int)tun uinal:(int)uinal kin:(int)kin
{
	NSCalendarDate *endDate = [TimeKit endDate];

	NSTimeInterval nst=[self longCount: baktun katun:katun tun:tun uinal:uinal kin:kin]*oneDay;
	nst-=(LONGCOUNT_DAYS*oneDay);
	return [endDate addTimeInterval:nst];
}

+(NSCalendarDate*)daysRemainingToNSCalendarDate:(int)days
{
	NSCalendarDate *endDate = [TimeKit endDate];
	NSTimeInterval nst=days*oneDay;
	nst-=(LONGCOUNT_DAYS*oneDay);

	return [endDate addTimeInterval:nst];
}


- (int)longCountFromDate:(int)month day:(int)day year:(int)year
{
	NSCalendarDate *startDate = [NSCalendarDate dateWithYear:year month:month day:day hour:00 minute:00 second:00 timeZone:[NSTimeZone systemTimeZone]];
	
	NSTimeInterval interval = [[TimeKit endDate] timeIntervalSinceDate: startDate];
	
	int days=interval/oneDay;

	return LONGCOUNT_DAYS-days;
}



-(int)kin
{
	return _kin;
}

-(int)uinal
{
	return _uinal;
}

-(int)tun
{
	return _tun;
}

-(int)katun
{
	return _katun;
}

-(int)baktun
{
	return _baktun;
}

-(void)initDays:(int)days
{
	_days=days;

	_baktun = days / 144000;
    days = days % 144000;
    _katun = days / 7200;
    days = days % 7200;
    _tun = days / 360;
    days = days % 360;
    _uinal = days / 20;
    _kin = days % 20;
	
}

-(int)daysElapsed
{
	return _days;
}

- (int)daysRemaining
{
	return LONGCOUNT_DAYS-_days;
}

-(int)tzolkinMonths
{
	int days=(_days-1)%20;
	
	if (days>19) days=0;
	if (days <0) days=19;
	return days;
}

-(int)tzolkinDays
{
	int day= (_days+4)%13;
	if (day==0) day=13;
	
	return day;
}

-(int)haabDays
{
    int day = _days + 8 + ((18 - 1) * 20);
	day=day % 365;
	return (day%20);
}

-(int)haabMonths
{
    int day = _days + 8 + ((18 - 1) * 20);
	day=day % 365;
	return (day/20)+1;

}

- (NSString *)haabSign
{
	return [NSString stringWithFormat:@"%d %s", [self haabDays], MAYAN_HAAB_MONTHS[[self haabMonths]-1]];
}


-(NSString *)tzolkinSign
{
	return [NSString stringWithFormat:@"%s %s (%s)", TONESWORDS[[self tzolkinDays]-1], MAYAN_TZOLKIN_MONTHS[[self tzolkinMonths]], DREAMSPELL_TZOLKIN_MONTHS[[self tzolkinMonths]]];
}

-(NSString*)dreamspellLongCountSign
{
	return [NSString stringWithFormat:@"%s %s %s", DREAMSPELL_TZOLKIN_COLORS[[self tzolkinMonths]], DREAMSPELL_TONES[[self tzolkinDays]-1], DREAMSPELL_TZOLKIN_MONTHS[[self tzolkinMonths]]];
}



@end
